/**
 * Copyright (C) 2009 Bruno Dumas (bruno.dumas -at- unifr.ch), DIVA group, University of Fribourg, Switzerland
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */



package diuf.diva.hephaistk.xml;

import it.unina.lab.costants.Constants;

import java.io.Serializable;
import java.util.ArrayList;


public class Element extends XmlGeneric implements Serializable {


	private static final long serialVersionUID = 347678155770858479L;
	private String namespace = null;
	private String name = null;
	private int id = -1;

	protected ArrayList <XmlGeneric> elements = null;
	protected ArrayList <Attribute> attributes = null;
	protected ArrayList <CData> cData = null;

	public Element(String name, int id){
		this(name);
		this.id = id;
	}

	public Element(String name){
		this.name = name;
		elements = new ArrayList<XmlGeneric>();
		attributes = new ArrayList<Attribute>();
		cData = new ArrayList<CData>();
	}

	public Element(){
		this("_unknown_name_");
	}

	public void setNamespace(String namespace){
		this.namespace = namespace;
	}

	public String getNamespace(){
		return namespace;
	}

	public void addElement(XmlGeneric element) throws HephaisTKParsingException{
		elements.add(element);
	}

	public XmlGeneric getElement(int i){
		return elements.get(i);
	}

	public int elementsSize(){
		return elements.size();
	}

	public ArrayList<XmlGeneric> getElements(){
		return elements;
	}

	public void addAttribute(Attribute attribute) throws HephaisTKParsingException{
		attributes.add(attribute);
	}

	public Attribute getAttribute(int i){
		return attributes.get(i);
	}

	public int attributesSize(){
		return attributes.size();
	}

	public ArrayList<Attribute> getAttributes(){
		return attributes;
	}

	public Attribute getAttribute(String name){
	    for(Attribute a : attributes) {
			if (a.getName().equals(name)) {
				return a;
			}
		}
	    return null;
	  }


	/**
	 * @return Returns the name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name The name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public void addAttribute(String name, String value) throws HephaisTKParsingException {
		attributes.add(new Attribute(name, value));

	}

	public void addCData(String cData){
		this.cData.add(new CData(cData));
	}

	public void addCData(CData cData){
		this.cData.add(cData);
	}

	public CData getCData(int i){
		return this.cData.get(i);
	}

	public ArrayList<CData> getCDatas(){
		return cData;
	}

	public int getCDatasSize(){
		return cData.size();
	}

	@Override
	public String toString(int indent) {
		StringBuilder ret = new StringBuilder();
		for(int i = 0; i < indent; i++){
			ret.append(' ');
		}
		if(namespace != null){
			ret.append("<"+namespace+":"+name);
		}else{
			ret.append("<"+name);
		}
		for(int i = 0; i < attributes.size(); i++){
			ret.append(" "+attributes.get(i).toString());
		}
		ret.append(">");
		for(int i = 0; i < cData.size(); i++){
			ret.append(cData.get(i));
		}
		ret.append("\n");
		for(int i = 0; i < elements.size(); i++){
			ret.append(elements.get(i).toString(indent+Constants.INDENT_PREFIX));
		}
		for(int i = 0; i < indent; i++){
			ret.append(' ');
		}
		if(namespace != null){
			ret.append("</"+namespace+":"+name+">\n");
		}else{
			ret.append("</"+name+">\n");
		}
		return ret.toString();
	}

	/**
	 * @return Returns the id.
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id The id to set.
	 */
	public void setId(int id) {
		this.id = id;
	}

}
